/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.plugin.projectbrowser;

import gride.project.GrideScript;
import gride.project.Project;
import gride.project.ProjectManager;
import java.awt.Component;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author lukasz
 */
public class TooltipCellRenderer extends DefaultTreeCellRenderer
{
    private Project project;
    private DateFormat df;

    /**
     * 
     * @param project
     */
    public void setProject(Project project)
    {
        this.project = project;
        df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
         boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
        if (value instanceof ProjectTreeNode)
        {
            ProjectTreeNode node = (ProjectTreeNode) value;

            if (node.getType() == ProjectTreeNode.SCRIPT)
            {
                GrideScript script = (GrideScript) node.getObject();

                StringBuilder sb = new StringBuilder("<html>");

                sb.append("Script Name: <b>" + script.getName() + "</b><br/>");

                String pack = script.getPackage();
                sb.append("Package: <b>" +
                          (!pack.equals("") ? pack : "default") + "</b><br/>");

                sb.append("<hr/>");

                String sourcePath = project.getProjectDir().getAbsolutePath();
                sb.append("Source: <b>" + 
                          script.getSourcefile().getAbsolutePath().
                          replace(sourcePath + "/", "") + "</b><br/>");

                String date =
                     df.format(new Date(script.getSourcefile().lastModified()));
                sb.append("Last saved: <b>" + date + "</b><br/>");
                
                sb.append("Modified since: <b>" + script.isModified() + 
                          "</b><br/>");

                sb.append("<hr/>");

                if (script.getClassFile() != null &&
                    script.getClassFile().exists())
                {
                    String buildPath =project.getProjectDir().getAbsolutePath();
                    sb.append("Class file: <b>" +
                              script.getClassFile().getAbsolutePath().
                              replace(buildPath + "/", "") + "</b><br/>");

                    String date2 =
                      df.format(new Date(script.getClassFile().lastModified()));
                    sb.append("Last compiled: <b>" + date2 + "</b><br/>");

                    boolean modified = (script.isModified() ||
                                        (script.getSourcefile().lastModified() >
                                         script.getClassFile().lastModified()));
                    sb.append("Modified since: <b>" + modified + "</b><br/>");
                }

                sb.append("</html>");
                setToolTipText(sb.toString());
            }
            else
                setToolTipText(null);
        }

        return super.getTreeCellRendererComponent(tree, value, sel, expanded,
                                                           leaf, row, hasFocus);
    }
}
